//
//  ScoreAnalysisViewController.m
//  LiveSoftware
//
//  Created by lanou3g on 15/7/14.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "ScoreAnalysisViewController.h"


//联赛积分排名
#import "TitleForLeagueScoreRankViewCell.h"

//杯赛积分 标题
#import "TitleForLeagueRankViewCell.h"

//球队和标题
#import "TeamAndTitleTableViewCell.h"

//联赛积分排名
#import "LeagueScoreRankModel.h"

//杯赛积分排名
#import "LeagueScoreScoreModel.h"

//过往赛季
#import "PassVCScoreModel.h"

//cell.
//联赛积分排名cell
#import "LeagueScoreRankViewCell.h"

//杯赛排名cell
#import "LeagueRankViewCell.h"

//对赛往绩和近期cell
#import "PassVCScoreViewCell.h"


@interface ScoreAnalysisViewController ()



@property(nonatomic,copy)NSString *scoreData;

/**
 * 主队近期数据
 */
@property(nonatomic,copy)NSString *homeLastScore;
/**
 * 客队近期数据
 */
@property(nonatomic,copy)NSString *awayLastScore;

/**
 * 杯赛排名数据
 */
@property(nonatomic,copy)NSString *leagueScoreRank;

/**
 * 对赛往绩数据
 */
@property(nonatomic,copy)NSString *pastVSScore;

/**
 * 主队联赛排名数据
 */
@property(nonatomic,copy)NSString *homeLeagueRank;

/**
 *客队联赛排名数据
 */
@property(nonatomic,copy)NSString *awayLeagueRank;


/**
 *是否有对往赛季
 */
@property(nonatomic)BOOL isHasPassVC;

/**
 *是否有杯赛排名
 */
@property(nonatomic)BOOL isHasLeagueRank;

/**
 *是否有联赛积分
 */
@property(nonatomic)BOOL isHasScoreRank;



/**
 *联赛积分Key
 */
@property(nonatomic,strong)NSArray *leagueRankKeysArray;


/**
 *杯赛积分Key
 */

@property(nonatomic,strong)NSArray *leagueScoreKeysArray;


/**
 *对赛往绩key
 */
@property(nonatomic,strong)NSArray *passVCScoreKeysArray;


//近期成绩key
@property(nonatomic,strong)NSArray *lastScoreKeysArray;



/**
 *主队联赛积分ModelArray
 */
@property(nonatomic,strong)NSMutableArray *homeLeagueRankModelArray;


/**
 *客队联赛积分排名ModelArray
 */
@property(nonatomic,strong)NSMutableArray *awayLeagueRankModelArray;



/**
 *杯赛积分ModelArray
 */
@property(nonatomic,strong)NSMutableArray *leagueScoreRankModelArray;



/**
 *对赛往绩ModelArray
 */
@property(nonatomic,strong)NSMutableArray *passVCScoreModelArray;

/**
 *主队近期ModelArray
 */
@property(nonatomic,strong)NSMutableArray *HomeLastScoreModelArray;

/**
 *客队近期ModelArray
 */
@property(nonatomic,strong)NSMutableArray *awayLastScoreModelArray;





@property(nonatomic,copy)NSString *sectionZeroTitle;

@property(nonatomic,strong)UIView *noDataView;



@end

@implementation ScoreAnalysisViewController



- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
    
    
    //联赛积分
    self.leagueRankKeysArray = @[@"overall",@"game",@"win",@"draw",@"loss",@"get",@"miss",@"score",@"rank",@"winProp"];
    
    
    //杯赛积分
    self.leagueScoreKeysArray =@[@"rank",@"team",@"total",  @"win",@"draw",@"loss",@"get",@"miss",@"clean",@"score"];
    
    //近期
    self.lastScoreKeysArray = @[@"date",@"leagueName",@"homeTeam",@"awayTeam",@"homeAllScore",@"awayAllScore",@"winOrLoss",@"homeID",@"awayID",@"HomeHalfScore",@"awayHalfScore",@"yapan"];
    
    //对赛往绩
    self.passVCScoreKeysArray =@[@"date",@"leagueName",@"homeTeam",@"awayTeam",@"homeAllScore",@"awayAllScore",@"HomeHalfScore",@"awayHalfScore",@"homeID",@"awayID",@"winOrLoss",@"yapan"];
    
    
    
    [self JieXiForScoreAnalysis];
    
    
    
    // 添加空白的 footView , 防止界面显示不全
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenIp6(5))];
    self.tableView.tableFooterView = footerView;
    
    
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    // 设置 tableView 不能拖动
    self.tableView.scrollEnabled = NO;
    
    
}

-(NSString*)getCurentTime
{
    
    
    NSDate *today = [NSDate date];
    
    double dureen = [today timeIntervalSince1970];
    
    NSString *timeString = [NSString stringWithFormat:@"%f",dureen];
    
    
    NSString* miao = [[timeString componentsSeparatedByString:@"."]objectAtIndex:0];
    NSString *haomiao = [[timeString componentsSeparatedByString:@"."]objectAtIndex:1];
    
    //    NSLog(@"---miao--%@",miao);
    //    NSLog(@"--haomiao---%@",haomiao);
    
    NSString *subHaomiao =[haomiao substringToIndex:3];
    
    NSString *curentTime = [NSString stringWithFormat:@"%@%@",miao,subHaomiao];
    
    return curentTime;
    
    
}


-(void)JieXiForScoreAnalysis
{
    
    //首先的到matchID
    //此次需要四个参数
    //1.
    NSString *firstString = [self.matchID substringToIndex:1];
    NSString *secondString = [self.matchID substringWithRange:NSMakeRange(1, 2)];
    
    
    //    NSLog(@"--------------分析网址-------------------------------");
    NSString *urlString = [NSString stringWithFormat:@"http://txt.win007.com/phone/analysis/%@/%@/cn/%@.htm?ran=%@", firstString,secondString, self.matchID, [self getCurentTime ]];
    //    NSLog(@"-----urlString-----%@",urlString);
    
    NSURL *url = [NSURL URLWithString:urlString];
    
    //异步解析
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
    
    
    [urlRequest addValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
    
    [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue]completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
     {
         
         if(data ==nil || response ==nil )
         {
             return ;
         }
         self.scoreData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
         
         [self divisionData];
         [self sectionZeroHeaderTitle];
         
         [self.tableView reloadData];
         
#pragma mark - 解析完数据, 刷新表视图之后, 发送通知
         [[NSNotificationCenter defaultCenter] postNotificationName:@"analysisFinish" object:nil];
         
         //         NSLog(@"%f",self.tableView.contentSize.height);
         
     }];
    
}



//分离数据
-(void)divisionData
{
    
#pragma mark  赛事筛选
    
    
    if(self.scoreData.length != 0 && self.scoreData != nil)
    {
        
        
//        NSLog(@"\n----self.scoreData---->%@---\n",self.scoreData);
        
        NSMutableArray *first = [[self.scoreData componentsSeparatedByString:@"$$"]mutableCopy];
        
//        NSLog(@"\n-----first-----%@\n",first);
        
        [first removeObject:@""];
        [first removeObject:@"^^"];
        
        
        
        // 0. "$$$$$$$$$$$$$$$$$$" 代表没有数据
        if([[self.scoreData substringToIndex:18] isEqualToString:@"$$$$$$$$$$$$$$$$$$"])
        {
            
            
            self.noDataView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
            self.noDataView.backgroundColor = [UIColor whiteColor];
            
            
            
            UIImageView *pointPicture = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth/2 - kScreenIp6(100), kScreenIp6(100), kScreenIp6(200), kScreenIp6(200))];
            pointPicture.image = [UIImage imageNamed:@"Empty_Hint_ZZZ@2x"];
            
            [ self.noDataView addSubview:pointPicture];
            
            
            [self.view addSubview: self.noDataView];
            
            
        }
        
        // 1. "$$$$$$$$" 代表第一个是 近期赛绩
        else if([[self.scoreData substringToIndex:8] isEqualToString:@"$$$$$$$$"])
        {
            
            
            // NSLog(@"--------------------------first.count--%lu",first.count);
            
            self.homeLastScore = first[0];
            self.awayLastScore = first[1];
            
            if([[[first lastObject] substringToIndex:3] isEqualToString:@"1^^" ])
            {
                self.leagueScoreRank = [first lastObject];
                self.isHasLeagueRank = YES;
            }
            else
            {
                self.isHasLeagueRank = NO; // 杯赛积分排名
            }
            
            self.isHasPassVC = NO; // 对赛往绩  无
            self.isHasScoreRank = NO;// 联赛积分排名  无
            
        }
        
        // 2. "$$$$$$" 代表第一个是 对赛往绩
        else if([[self.scoreData substringToIndex:6] isEqualToString:@"$$$$$$"])
        {
            
            self.pastVSScore = first [0]; // 对赛往绩
            self.homeLastScore = first[1]; // 主队近期
            self.awayLastScore = first[2]; // 客队近期
            
            if([[[first lastObject] substringToIndex:3] isEqualToString:@"1^^" ])
            {
                self.leagueScoreRank = [first lastObject];
                self.isHasLeagueRank = YES;
            }
            else
            {
                self.isHasLeagueRank = NO; // 杯赛排名 无
            }
            
            self.isHasPassVC = YES; // 对赛往绩  有
            self.isHasScoreRank = NO;// 积分排名  无
            
        }
        
        /*
         //1.  主队排名 >> 客队排名 >> 对赛往绩 >> 主队近期 >> 客队近期
         //2.  主队排名 >> 对赛往绩 >> 主队近期 >> 客队近期
         //3.  主队排名 >> 主队近期 >> 客队近期
         //4   主队排名 >> 客队排名 >> 主队近期 >> 客队近期
         */
        
        // 3. 以"总"开头 代表第一个是 积分排名
        else if([[self.scoreData substringToIndex:1] isEqualToString:@"总"])
        {
            self.isHasScoreRank = YES; //积分排名  无
            self.isHasLeagueRank = NO; //杯赛排名  有
            
            
            
            self.homeLeagueRank = first[0]; // 主队积分排名
            
            
            
            //查找字符串 $$$$$$ 近期赛绩
            NSInteger awayScoreRankInt =[self.scoreData  rangeOfString:@"$$总" ].location;//有客队积分排名6090381
            
            NSInteger passVCScoreInt =[self.scoreData  rangeOfString:@"$$$$20" ].location;//对赛往   199
            
            NSInteger lastScoreInt =[self.scoreData  rangeOfString:@"$$$$$$20" ].location;//近期   197
            
            
            //3.  主队>>主队近期 >>客队近期
            //近期          对赛                   近期          客队
            if(lastScoreInt < passVCScoreInt &&lastScoreInt < awayScoreRankInt )//有近期,没有客队排名,没有对赛
            {
                
                
                self.homeLastScore = first[1];//主队近期
                self.awayLastScore =first[2]; //客队近期
                
                self.isHasPassVC =NO; //对赛往绩  无
                
                
            }
            //2.主队>>对赛往绩>>主队近期>>客队近期
            //对赛             //近期          //对赛             //客队排名
            else  if(passVCScoreInt < lastScoreInt && passVCScoreInt < awayScoreRankInt&& lastScoreInt < awayScoreRankInt)// 有对赛,有近期,无客队
            {
                
                self.pastVSScore = first[1];//对往赛绩
                self.homeLastScore = first[2];//主队 -近期
                self.awayLastScore = first[3];//客队近期
                
                
                self.isHasPassVC =YES; //对赛往绩  无
                
                
            }
            
            //4.主队排名>>客队排名>>  主队近期>>客队近期
            //客队排名            //近期
            else if(awayScoreRankInt < lastScoreInt && awayScoreRankInt < passVCScoreInt)  //  有客队排名
                
            {
                self.awayLeagueRank = first[1];//客队-联赛排名
                
                
                
                //近期          //对赛
                if(lastScoreInt < passVCScoreInt)  //没有对赛
                    
                {
                    self.homeLastScore = first[2];//主队近期
                    self.awayLastScore = first[3];//客队近期
                    
                    self.isHasPassVC =NO; //对赛往绩  无
                    
                }
                
                //主队>>客队>>对赛>>主队近期>>客队近期
                //对赛              //近期
                if(passVCScoreInt < lastScoreInt  &&  awayScoreRankInt < passVCScoreInt) //有对赛
                {
                    self.pastVSScore = first[2];//对赛往绩
                    self.homeLastScore = first[3];//主队近期
                    self.awayLastScore = first[4];//客队近期
                    
                    self.isHasPassVC =YES; //对赛往绩  有                 }
                }
            }
            
        }
        else if([[self.scoreData substringToIndex:3] isEqualToString:@"$$总"]) //没有主队 ,有客队积分排名
        {
            self.awayLeagueRank = first[0];
            
            self.isHasScoreRank = YES;//积分排名  有
            self.isHasLeagueRank = NO; //杯赛排名  NO
            
            // 1.客队  >>对赛 >> 主队近期 >>客队近期
            // 2.客队 >> 主队近期>>客队近期
            //
            
            //查找字符串 $$$$$$   近期赛绩
            NSInteger awayScoreRankInt =[self.scoreData  rangeOfString:@"$$总" ].location;//有客队积分排名6090381
            
            NSInteger passVCScoreInt =[self.scoreData  rangeOfString:@"$$$$20" ].location;//对赛往
            
            NSInteger lastScoreInt =[self.scoreData  rangeOfString:@"$$$$$$20" ].location;//近期
            
            
            //第二种情况
            /// 2.客队 >> 主队近期>>客队近期
            
            //近期         //对赛           //近期            //客队
            if(lastScoreInt < passVCScoreInt &&lastScoreInt > awayScoreRankInt )//有近期,有客队
            {
                
                
                self.homeLastScore = first[1];//主队近期
                self.awayLastScore =first[2]; //客队近期
                
                self.isHasPassVC =NO; //对赛往绩  无
                
                
            }
            // 1.客队  >>对赛 >> 主队近期 >>客队近期
            //有对赛       //近期           //对赛            //客队
            else  if(passVCScoreInt < lastScoreInt && passVCScoreInt > awayScoreRankInt)//有对赛
            {
                
                self.pastVSScore = first[1];//对往赛绩
                self.homeLastScore = first[2];//主队 -近期
                self.awayLastScore = first[3];//客队近期
                
                
                self.isHasPassVC =YES; //对赛往绩  有
                
                
            }
            
            
            
        }
        
        
        
        
        
        // NSMutableArray *fenzu = [[first[0] componentsSeparatedByString:@"!"]mutableCopy];
        
        /*
         NSLog(@"\n===================================================================");
         
         NSLog(@"-----联赛积分--***主队------%@",self.homeLeagueRank);
         NSLog(@"-----联赛积分--***客队------%@",self.awayLeagueRank);
         
         NSLog(@"-----杯赛排名--------%@",self.leagueScoreRank);
         
         NSLog(@"-----对赛往绩--------%@",self.pastVSScore);
         
         NSLog(@"-----主队近期--------%@",self.homeLastScore);
         
         NSLog(@"-----客队近期--------%@",self.awayLastScore);
         
         NSLog(@"===================================================================\n");
         */
        
#pragma mark  联赛积分排名
        //主队
        
        
        if(self.homeLeagueRank !=nil && self.homeLeagueRank.length > 20)
        {
            
            self.homeLeagueRankModelArray = [NSMutableArray array];
            
            
            //下面的数据
            
            NSArray *totalRow= [self.homeLeagueRank componentsSeparatedByString:@"!"];
            
            for(int  i= 0; i< totalRow.count ;i++)
            {
                NSMutableArray *everyRow = [[totalRow[i] componentsSeparatedByString:@"^"]mutableCopy];
                
                if(everyRow.count>10)
                {
                    [everyRow removeLastObject];
                }
                
                
                
                NSDictionary *rowDict = [NSDictionary dictionaryWithObjects:everyRow forKeys:self.leagueRankKeysArray];
                
                LeagueScoreRankModel *model = [LeagueScoreRankModel LeagueScoreRankModelWithDict:rowDict];
                [self.homeLeagueRankModelArray addObject:model];
            }
        }
        
        
        
        
        //客队
        if(self.awayLeagueRank !=nil && self.awayLeagueRank.length > 20  )
        {
            
            
            
            self.awayLeagueRankModelArray = [NSMutableArray array];
            
            NSArray *totalRow= [self.awayLeagueRank componentsSeparatedByString:@"!"];
            
            for(int  i= 0; i< totalRow.count ;i++)
            {
                NSMutableArray *everyRow = [[totalRow[i] componentsSeparatedByString:@"^"]mutableCopy];
                
                if(everyRow.count>10)
                {
                    [everyRow removeLastObject];
                }
                
                NSDictionary *rowDict = [NSDictionary dictionaryWithObjects:everyRow forKeys:self.leagueRankKeysArray];
                
                LeagueScoreRankModel *model = [LeagueScoreRankModel LeagueScoreRankModelWithDict:rowDict];
                [self.awayLeagueRankModelArray addObject:model];
            }
        }
        
        
#pragma mark  杯赛积分排名
        
        
        if(self.leagueScoreRank !=nil &&self.leagueScoreRank.length >20)
        {
            
            self.isHasLeagueRank = YES; //杯赛排名  有
            
            self.leagueScoreRankModelArray = [NSMutableArray array];
            
            
            
            NSArray *totalRow= [self.leagueScoreRank componentsSeparatedByString:@"!"];
            
            for(int  i= 0; i< totalRow.count ;i++)
            {
                
                
                NSMutableArray *everyRow = [[totalRow[i] componentsSeparatedByString:@"^"]mutableCopy];
                
#warning 杯赛的数据  不等于10
                if(everyRow.count != 10)
                {
                    
                    return;
                }
                
                [everyRow removeObjectAtIndex:1];
                int clean = [everyRow[6]intValue] -[everyRow[7]intValue];
                [everyRow insertObject:[NSString stringWithFormat:@"%d",clean] atIndex:8];
                
                NSDictionary *rowDict = [NSDictionary dictionaryWithObjects:everyRow forKeys:self.leagueScoreKeysArray];
                
                LeagueScoreScoreModel *model = [LeagueScoreScoreModel LeagueScoreScoreModelWithDict:rowDict];
                [self.leagueScoreRankModelArray addObject:model];
            }
        }
        
        
        
#pragma mark   对赛往绩
        
        
        if(self.pastVSScore !=nil  &&  self.pastVSScore.length > 20)
        {
            //初始化ModelArray
            self.passVCScoreModelArray = [NSMutableArray array];
            
            NSArray *totalRow= [self.pastVSScore componentsSeparatedByString:@"!"];
            
            for(int  i= 0; i< totalRow.count ;i++)
            {
                NSMutableArray *everyRow = [[totalRow[i] componentsSeparatedByString:@"^"]mutableCopy];
                
#warning 判断对往赛季的
                // 20130928130000^沙滩世杯^大溪地沙滩足球队^俄罗斯沙滩足球队^3^5^0^0^17925^6792^L^-2.5!
                
                
                if(everyRow .count !=12 )
                {
                    
                    if( everyRow .count !=11 )
                    {
                        return ;
                    }
                }
                
                
                everyRow[0]  = [everyRow[0] substringToIndex:8];
                
                
                if([[everyRow  objectAtIndex:10]isEqualToString:@"W"])
                {
                    everyRow[10] = @"赢";
                    
                    
                }
                else if([[everyRow  objectAtIndex:10]isEqualToString:@"L"])
                {
                    everyRow[10] = @"输";
                    
                }
                else if([[everyRow  objectAtIndex:10]isEqualToString:@"D"])
                {
                    
                    everyRow[10] = @"走";
                }
                
                else
                {
                    
                }
                
                NSDictionary *rowDict = [NSDictionary dictionaryWithObjects:everyRow forKeys:self.passVCScoreKeysArray];
                
                
                
                PassVCScoreModel *model = [PassVCScoreModel PassVCScoreModelWithDict:rowDict];
                
                
                
#warning 添加---胜平负
                
                
                [self.passVCScoreModelArray addObject:model];
            }
        }
        
        
        
        
        
        
#pragma mark  近期战绩
        
        
        //主队
        
        
        
        if(self.homeLastScore !=nil  && self.homeLastScore.length > 20)
        {
            //初始化ModelArray
            self.HomeLastScoreModelArray = [NSMutableArray array];
            
            
            
            NSArray *totalRow= [self.homeLastScore componentsSeparatedByString:@"!"];
            
            for(int  i= 0; i< totalRow.count ;i++)
            {
                NSMutableArray *everyRow = [[totalRow[i] componentsSeparatedByString:@"^"]mutableCopy];
                
#warning 判断近期赛绩--主队
                
                //$$20150712213000^沙滩世杯^马达加斯加沙滩足球队(中)^俄罗斯沙滩足球队^2^4^输^17103^6792^0^0^-2.5!
                
                if(everyRow .count !=12 )
                {
                    
                    if( everyRow .count !=11 )
                    {
                        return ;
                    }
                }
                
                everyRow[0]  = [everyRow[0] substringToIndex:8];
                
                NSDictionary *rowDict = [NSDictionary dictionaryWithObjects:everyRow forKeys:self.lastScoreKeysArray];
                
                PassVCScoreModel *model = [PassVCScoreModel PassVCScoreModelWithDict:rowDict];
                [self.HomeLastScoreModelArray addObject:model];
            }
        }
        
        
        
        
        
        //客队
        
        if(self.awayLastScore !=nil && self.awayLastScore.length >20)
        {//初始化ModelArray
            self.awayLastScoreModelArray = [NSMutableArray array];
            
            
            
            
            NSArray *totalRow= [self.awayLastScore componentsSeparatedByString:@"!"];
            
            for(int  i= 0; i< totalRow.count ;i++)
            {
                NSMutableArray *everyRow = [[totalRow[i] componentsSeparatedByString:@"^"]mutableCopy];
                
                
#warning 判断近期赛绩--客队
                
                //$$20150712213000^沙滩世杯^马达加斯加沙滩足球队(中)^俄罗斯沙滩足球队^2^4^输^17103^6792^0^0^-2.5!
                
                
                if(everyRow .count !=12 )
                {
                    
                    if( everyRow .count !=11 )
                    {
                        return ;
                    }
                }
                
                everyRow[0] = [everyRow[0] substringToIndex:8];
                
                
                NSDictionary *rowDict = [NSDictionary dictionaryWithObjects:everyRow forKeys:self.lastScoreKeysArray];
                
                PassVCScoreModel *model = [PassVCScoreModel PassVCScoreModelWithDict:rowDict];
                [self.awayLastScoreModelArray addObject:model];
                
            }
            
        }
        
    }
    else
    {
        
        UIView *elseNoDataView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        elseNoDataView.backgroundColor = [UIColor whiteColor];
        
        
        
        UIImageView *pointPicture = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth/2 - kScreenIp6(100), kScreenIp6(100), kScreenIp6(200), kScreenIp6(200))];
        pointPicture.image = [UIImage imageNamed:@"Empty_Hint_ZZZ@2x"];
        
        [ elseNoDataView addSubview:pointPicture];
        
        
        [self.view addSubview: elseNoDataView];
        
        
        
    }
    
    
}


- (void)sectionZeroHeaderTitle
{
    if (!self.isHasScoreRank && !self.isHasLeagueRank && self.isHasPassVC)
    {
        self.sectionZeroTitle = @"对赛往绩";
    }
}


#pragma mark - 自定义 SectionHeaderView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    SectionHeaderView *headerView = [[SectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenIp6(30))];
    
    if (section == 0)
    {
        if(self.isHasScoreRank)
        {
            headerView.headerLabel.text = @"联赛积分排名";
        }
        else  if([self isHasLeagueRank])
        {
            headerView.headerLabel.text = @"杯赛积分排名";
        }
        else  if(self.isHasPassVC)
        {
            headerView.headerLabel.text = @"对赛往绩";
        }
        else
        {
            headerView.headerLabel.text = @"近期战绩";
        }
        
        
        
    }
    else if (section == 1)
    {
        
#pragma mark  判断 上一个分区的名字是不是对赛
        if ([self.sectionZeroTitle isEqualToString:@"对赛往绩"]) {
            
            headerView.headerLabel.text = @"近期战绩";
            
        }
        else
        {
            if(self.isHasPassVC)
            {
                headerView.headerLabel.text = @"对赛往绩";
            }
            else
            {
                headerView.headerLabel.text = @"近期战绩";
            }
        }
        
    }
    else
    {
        headerView.headerLabel.text =  @"近期战绩";
    }
    
    
    return headerView;
}


#pragma mark SectionHeaderView 的 高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return kScreenIp6(30);
}



#pragma mark  返回cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        
        if (self.isHasScoreRank) // 有联赛积分
        {
            if (indexPath.row == 0)
            {
                return 70;
            }
            else if(indexPath.row == self.homeLeagueRankModelArray.count + 1)
            {
                return 70;
            }
            else
            {
                return 35;
            }
            
        }
        else if(self.isHasLeagueRank) // 杯赛排名
        {
            if(indexPath.row == 0)
            {
                return 35;
            }
            else
            {
                return 35;
            }
        }
        
        else  if(self.isHasPassVC) // 对战
        {
            
            if(indexPath.row == 0) // 对战
            {
                return 70;
            }
            else
            {
                return 40;
            }
        }
        else // 近期
        {
            if(indexPath.row == 0) // 主队
            {
                
                return 70;
            }
            
            else if(indexPath.row == self.HomeLastScoreModelArray.count + 1) // 客队
            {
                return 70;
            }
            else
            {
                return 40;
            }
            
        }
        
    }
    else if(indexPath.section == 1)
    {
        
        
#pragma mark  判断 上一个分区的名字是不是对赛
        if ([self.sectionZeroTitle isEqualToString:@"对赛往绩"])
        {
            
            if(indexPath.row == 0) // 主队
            {
                return 70;
            }
            else if(indexPath.row == self.HomeLastScoreModelArray.count + 1) // 客队
            {
                return 70;
            }
            else
            {
                return 40;
            }
        }
        else
        {
            
            if(self.isHasPassVC) // 对战
            {
                
                if(indexPath.row == 0)
                {
                    return 70;
                }
                else
                {
                    return 40;
                }
                
            }
            
            else // 近期
            {
                if(indexPath.row == 0) // 主队
                {
                    return 70;
                }
                else if(indexPath.row == self.HomeLastScoreModelArray.count + 1) // 客队
                {
                    return 70;
                }
                else
                {
                    return 40;
                }
            }
            
        }
        
        
        
    }
    else // section == 2; 近期
    {
        if(indexPath.row == 0) // 主队
        {
            return 70;
        }
        else if(indexPath.row == self.HomeLastScoreModelArray.count + 1) // 客队
        {
            return 70;
        }
        
        else
        {
            return 40;
        }
    }
    
}



#pragma mark - Table view data source 数据源方法

- (NSInteger)numberofSection
{
    NSInteger num = 0;
    if(self.isHasScoreRank )//是否有联赛积分
    {
        
        num = num +1;
        
    }
    if(self.isHasLeagueRank)//是否有杯赛排名
    {
        
        num = num + 1;
        
    }
    if(self.isHasPassVC)//是否有对往赛季
    {
        
        num = num +1;
        
    }
    
    //再加1是 加 近期
    num = num +1;
    
    return num;
    
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return [self numberofSection];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    if (section == 0)
    {
        if(self.isHasScoreRank) //联赛积分排名
        {
            
            return self.homeLeagueRankModelArray.count + self.awayLeagueRankModelArray.count + 2;
            
        }
        else if(self.isHasLeagueRank)   //杯赛积分排名
        {
            
            return self.leagueScoreRankModelArray.count + 1;
            
        }
        else if(self.isHasPassVC) //对往战绩
            
        {
            return self.passVCScoreModelArray.count + 1;
        }
        else //只有近期
        {
            return self.HomeLastScoreModelArray.count + self.awayLastScoreModelArray.count + 2;
        }
        
    }
    else if(section == 1)
    {
        if ([self.sectionZeroTitle isEqualToString:@"对赛往绩"])
        {
            // 近期战绩
            return self.HomeLastScoreModelArray.count + self.awayLastScoreModelArray.count  + 2;
        }
        
        else
        {
            
            if(self.isHasPassVC) // 对赛往绩
            {
                return self.passVCScoreModelArray.count + 1;
            }
            else   // 近期战绩
            {
                
                return self.HomeLastScoreModelArray.count + self.awayLastScoreModelArray.count  + 2;
            }
            
        }
        
    }
    else // 近期战绩
    {
        
        return self.HomeLastScoreModelArray.count + self.awayLastScoreModelArray.count + 2 ;
    }
    
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        
#pragma mark 联赛积分排名
        if (self.isHasScoreRank) // 联赛积分
        {
            
            if(indexPath.row == 0) //row =  0
            {
                static NSString *cellIndetifier = @"TitleForleague";
                [tableView registerClass:[TitleForLeagueScoreRankViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                TitleForLeagueScoreRankViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                cell.teamName.text = self.homeName;
                
                return cell;
                
            }
            
            else  if (indexPath.row < self.homeLeagueRankModelArray.count + 1)//row =  1-4
            {
                static NSString *cellIndetifier = @"scoreRankCell";
                [tableView registerClass:[LeagueScoreRankViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                LeagueScoreRankViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                //主队的
                
                LeagueScoreRankModel*model = self.homeLeagueRankModelArray[indexPath.row -1];
                
                
                [cell setCellValue:model];
                
                
                return cell;
            }
            
            //客队的
            else  if(indexPath.row ==self.homeLeagueRankModelArray.count + 1)// row = 5
            {
                static NSString *cellIndetifier = @"TitleForleague";
                [tableView registerClass:[TitleForLeagueScoreRankViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                TitleForLeagueScoreRankViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                cell.teamName.text = self.awayName;
                
                return cell;
            }
            else //row =6-9
            {
                //                 NSLog(@"---indexPath.row=----%ld",indexPath.row);
                static NSString *cellIndetifier = @"scoreRankCell";
                [tableView registerClass:[LeagueScoreRankViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                LeagueScoreRankViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                LeagueScoreRankModel *model = self.awayLeagueRankModelArray[indexPath.row - self.homeLeagueRankModelArray.count -2];
                
                [cell setCellValue:model];
                
                return cell;
            }
            
            
        }
        
#pragma mark 杯赛积分排名
        else if(self.isHasLeagueRank)
        {
            
            
            if(indexPath.row == 0)
            {
                
                static NSString *cellIndetifier = @"leagueRankCell";
                [tableView registerClass:[TitleForLeagueRankViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                TitleForLeagueRankViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                
                return cell;
                
                
            }
            else
            {
                static NSString *cellIndetifier = @"laegueRankCell";
                [tableView registerClass:[LeagueRankViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                LeagueRankViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                NSLog(@"%ld",self.leagueScoreRankModelArray.count);
                if (self.leagueScoreRankModelArray.count > (indexPath.row-1) )
                {
                    
                    
                    LeagueScoreScoreModel*model = self.leagueScoreRankModelArray[indexPath.row-1];
                    
                    [cell setCellValue:model];
                }
                
                
                return cell;
            }
            
        }
        else  if(self.isHasPassVC) // 对往赛季
        {
            if(indexPath.row ==0)
            {
                static NSString *cellIndetifier = @"TeamAndTitleCell";
                [tableView registerClass:[TeamAndTitleTableViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                TeamAndTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                cell.teamName.text = self.homeName;
                
                return cell;
            }
            
            else
            {
                static NSString *cellIndetifier = @"passVC";
                [tableView registerClass:[PassVCScoreViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                PassVCScoreViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                PassVCScoreModel*model = self.passVCScoreModelArray[indexPath.row-1];
                
                [cell setCellValueWithModel:model name:self.homeName];
                
                return cell;
            }
            
        }
        
        else // 近期赛季
        {
            
            // 主队
            if(indexPath.row ==0)
            {
                static NSString *cellIndetifier = @"TeamAndTitleCell";
                [tableView registerClass:[TeamAndTitleTableViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                TeamAndTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                cell.teamName.text =self.homeName;
                
                return cell;
            }
            else if(indexPath.row < self.HomeLastScoreModelArray.count + 1)
            {
                static NSString *cellIndetifier = @"passVCCell2";
                [tableView registerClass:[PassVCScoreViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                PassVCScoreViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                
                PassVCScoreModel*model = self.HomeLastScoreModelArray[indexPath.row -1];
                
                [cell setCellValueWithModel:model name:self.homeName];
                
                return cell;
            }
            
            //客队
            else if(indexPath.row == self.HomeLastScoreModelArray.count + 1 )
            {
                
                
                static NSString *cellIndetifier = @"TeamAndTitleCell";
                [tableView registerClass:[TeamAndTitleTableViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                TeamAndTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                cell.teamName.text = self.awayName;
                
                return cell;
                
            }
            
            else
            {
                static NSString *cellIndetifier = @"passVCCell2";
                [tableView registerClass:[PassVCScoreViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                PassVCScoreViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                
                
                PassVCScoreModel*model = self.awayLastScoreModelArray[indexPath.row - self.HomeLastScoreModelArray.count -2];
                
                
                [cell setCellValueWithModel:model name:self.awayName];
                
                
                return cell;
                
            }//客队
            
            
        }
        
        
        
    }
    
    else if (indexPath.section == 1)
        
    {
        
#pragma mark  判断 上一个分区的名字是不是"对赛往绩"
        
        
        if ([self.sectionZeroTitle isEqualToString:@"对赛往绩"])
        {
            
#pragma mark 如果第0个分区为"对赛往绩" 则第1个分区肯定是"近期战绩"
            //主队
            if(indexPath.row == 0)
            {
                static NSString *cellIndetifier = @"TeamAndTitleCell";
                [tableView registerClass:[TeamAndTitleTableViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                TeamAndTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                cell.teamName.text = self.homeName;
                
                return cell;
            }
            
            else if(indexPath.row < self.HomeLastScoreModelArray.count + 1)
            {
                
                static NSString *cellIndetifier = @"passVCCell2";
                [tableView registerClass:[PassVCScoreViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                PassVCScoreViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                PassVCScoreModel*model = self.HomeLastScoreModelArray[indexPath.row - 1];
                
                
                [cell setCellValueWithModel:model name:self.homeName];
                
                return cell;
            }
            
            //客队
            else if(indexPath.row == self.HomeLastScoreModelArray.count + 1 )
            {
                
                static NSString *cellIndetifier = @"TeamAndTitleCell";
                [tableView registerClass:[TeamAndTitleTableViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                TeamAndTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                cell.teamName.text = self.awayName;
                
                return cell;
            }
            else
            {
                static NSString *cellIndetifier = @"passVCCell2";
                [tableView registerClass:[PassVCScoreViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                PassVCScoreViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                PassVCScoreModel *model = self.awayLastScoreModelArray[indexPath.row - self.HomeLastScoreModelArray.count - 2];
                
                [cell setCellValueWithModel:model name:self.awayName];
                
                
                return cell;
                
            }//客队
            
            
        }
        else
        {
            
#pragma mark 如果第0个分区不是"对赛往绩" 则第1个分区可能是"对赛往绩", 可能是"近期战绩"
            if (self.isHasPassVC) // 对赛往绩
            {
                
                if(indexPath.row == 0)
                {
                    static NSString *cellIndetifier = @"TeamAndTitleCell";
                    [tableView registerClass:[TeamAndTitleTableViewCell class] forCellReuseIdentifier:cellIndetifier];
                    TeamAndTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                    
                    cell.teamName.text = self.homeName;
                    
                    return cell;
                }
                
                else
                    
                {
                    
                    static NSString *cellIndetifier = @"passVCCell1";
                    [tableView registerClass:[PassVCScoreViewCell class] forCellReuseIdentifier:cellIndetifier];
                    PassVCScoreViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                    
                    PassVCScoreModel*model = self.passVCScoreModelArray[indexPath.row-1];
                    
                    [cell setCellValueWithModel:model name:self.homeName];
                    
                    return cell;
                }
                
            }
            
            else // 近期战绩
                
            {
                //主队
                if(indexPath.row == 0)
                {
                    static NSString *cellIndetifier = @"TeamAndTitleCell";
                    [tableView registerClass:[TeamAndTitleTableViewCell class] forCellReuseIdentifier:cellIndetifier];
                    
                    TeamAndTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                    
                    cell.teamName.text = self.homeName;
                    
                    return cell;
                }
                
                else if(indexPath.row < self.HomeLastScoreModelArray.count + 1)
                {
                    
                    static NSString *cellIndetifier = @"passVCCell2";
                    [tableView registerClass:[PassVCScoreViewCell class] forCellReuseIdentifier:cellIndetifier];
                    
                    PassVCScoreViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                    
                    PassVCScoreModel*model = self.HomeLastScoreModelArray[indexPath.row - 1];
                    
                    [cell setCellValueWithModel:model name:self.homeName];
                    
                    return cell;
                }
                
                //客队
                else if(indexPath.row == self.HomeLastScoreModelArray.count + 1 )
                {
                    
                    static NSString *cellIndetifier = @"TeamAndTitleCell";
                    [tableView registerClass:[TeamAndTitleTableViewCell class] forCellReuseIdentifier:cellIndetifier];
                    
                    TeamAndTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                    
                    cell.teamName.text = self.awayName;
                    
                    return cell;
                }
                else
                {
                    static NSString *cellIndetifier = @"passVCCell2";
                    [tableView registerClass:[PassVCScoreViewCell class] forCellReuseIdentifier:cellIndetifier];
                    
                    PassVCScoreViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                    
                    PassVCScoreModel *model = self.awayLastScoreModelArray[indexPath.row - self.HomeLastScoreModelArray.count - 2];
                    
                    [cell setCellValueWithModel:model name:self.awayName];
                    
                    
                    return cell;
                    
                }//客队
                
            }
            
        }
        
    }//section ==1
    
#pragma mark 如果有第2个分区 肯定是"近期战绩"
    
    else // section = 2; 近期战绩
    {
        
#pragma mark  近期战绩
        if(indexPath.row ==0)
        {
            
            static NSString *cellIndetifier = @"TeamAndTitleCell";
            [tableView registerClass:[TeamAndTitleTableViewCell class] forCellReuseIdentifier:cellIndetifier];
            TeamAndTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
            
            cell.teamName.text = self.homeName;
            
            return cell;
        }
        
        
        else  if (indexPath.row < self.HomeLastScoreModelArray.count+1)
        {
            
            static NSString *cellIndetifier = @"passVCCell3";
            [tableView registerClass:[PassVCScoreViewCell class] forCellReuseIdentifier:cellIndetifier];
            PassVCScoreViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
            
            
            PassVCScoreModel*model = self.HomeLastScoreModelArray[indexPath.row-1];
            
            [cell setCellValueWithModel:model name:self.homeName];
            
            return cell;
            
        }
        
        
        else  if(indexPath.row ==self.HomeLastScoreModelArray.count + 1)
        {
            
            static NSString *cellIndetifier = @"TeamAndTitleCell";
            [tableView registerClass:[TeamAndTitleTableViewCell class] forCellReuseIdentifier:cellIndetifier];
            TeamAndTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
            
            cell.teamName.text = self.awayName;
            
            return cell;
        }
        else
        {
            
            static NSString *cellIndetifier = @"passVCCell3";
            [tableView registerClass:[PassVCScoreViewCell class] forCellReuseIdentifier:cellIndetifier];
            PassVCScoreViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
            
            PassVCScoreModel*model = self.awayLastScoreModelArray[indexPath.row -self.HomeLastScoreModelArray.count-2];
            
            [cell setCellValueWithModel:model name:self.awayName];
            
            return cell;
            
        }
        
    }
    
}






- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //    self.tableView.userInteractionEnabled = NO;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //    self.tableView.userInteractionEnabled = YES;
}









@end
